package com.station.Activitys.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.station.Activitys.R;
import com.station.Activitys.adapter.base.BaseRecyclerViewAdapter;
import com.station.Activitys.bean.CheckUser;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：基站管理平台 手机app
 * 类描述：发布信息 适配器
 * 创建人：张飞祥
 * 创建时间：2018/9/4 8:59
 * 修改人：张飞祥
 * 修改时间：2018/9/4 8:59
 * 修改备注：
 */
public abstract class ReleasePeopleAdapter extends BaseRecyclerViewAdapter {

    //上下文
    private Context mContext;
    //数据源
    private List<CheckUser> mUserList = null;
    //全选 状态
    private boolean mIsSelectAll = true;


    /**
     * 构造方法
     * @param mContext
     * @param mUserList
     */
    public ReleasePeopleAdapter(Context mContext, List<CheckUser> mUserList) {
        super(mContext, mUserList);
        this.mContext = mContext;
        this.mUserList = mUserList;
    }

    @Override
    protected int getLayoutItemResId() {
        return R.layout.item_release_people;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof ReleasePeopleViewHolder) {

            //TODO 获取信息中心 viewHolder
            final ReleasePeopleViewHolder holder = (ReleasePeopleViewHolder) viewHolder;
            //TODO  将数据保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(position);

            /**
             * 数据填充
             */
            //当前 数据实体
            final CheckUser checkUser = mUserList.get(position);
            //设置名称
            holder.cbSelectUser.setText(checkUser.getUserName());
            //设置状态
            holder.cbSelectUser.setChecked(checkUser.isChecked());

            /**
             * 复选按钮 点击事件
             */
            holder.cbSelectUser.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked) {
                    //设置 数据选中状态
                    checkUser.setChecked(isChecked);
                    mUserList.set(position,checkUser);
                    //设置 按钮选中状态
                    holder.cbSelectUser.setChecked(isChecked);

                    //TODO 重装 数据到 布尔集合中
                    List<Boolean> selectTag = new ArrayList<>();
                    for (int i = 0; i < mUserList.size(); i++){
                        selectTag.add(mUserList.get(i).isChecked());
                    }
                    //包含值 false
                    boolean tag = false;
                    //TODO 动态判断 是否包含 false
                    if (selectTag.contains(tag))
                        mIsSelectAll = false;
                    else
                        mIsSelectAll = true;

                    //提供给外部数据 操作类
                    setOnItemChecked(checkUser,mIsSelectAll,isChecked);
                    //刷新 数据源
                    notifyDataSetChanged();
                }
            });

        }
    }

    /**
     * 提供给外部数据 操作类
     */
    public abstract void setOnItemChecked(CheckUser checkUser, boolean mIsSelectAll,boolean isChecked);

    @Override
    public RecyclerView.ViewHolder getViewHolder(View view) {
        return new ReleasePeopleViewHolder(view);
    }

    /**
     * ViewHolder
     */
    public class ReleasePeopleViewHolder extends RecyclerView.ViewHolder {

        //复选按钮
        @Bind(R.id.cb_select_user)
        CheckBox cbSelectUser;

        public ReleasePeopleViewHolder(View itemView) {
            super(itemView);
            //初始化 控件绑定
            ButterKnife.bind(this, itemView);
            //初始化 屏幕适配
            AutoUtils.autoSize(itemView);
        }
    }
}